<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class favorite extends CI_Controller {

	private $all_session;
	private $id;
	private $data;
	public $input;

	public function __construct()
    {
        parent::__construct();

        @$this->all_session = $this->session->all_userdata();  
        @$this->id = $this->all_session['login'][0]['mem_id'];    
    }

	public function index()
	{		
		$this->data['member'] = $this->main_model->get_member($this->id);

		//////////////////////////////////////////////////////
		//////////////////// pagination //////////////////////
		//////////////////////////////////////////////////////

		$this->load->library('pagination');
		$config['first_tag_open'] = $config['last_tag_open']= $config['next_tag_open']= $config['prev_tag_open'] = $config['num_tag_open'] = '<li>';
        $config['first_tag_close'] = $config['last_tag_close']= $config['next_tag_close']= $config['prev_tag_close'] = $config['num_tag_close'] = '</li>';
        $config['cur_tag_open'] = "<li><span><b>";
        $config['cur_tag_close'] = "</b></span></li>";

		$config['base_url'] = base_url().'index.php/favorite/index';
		$config['per_page'] = 50;
				 
		$per_page = $config['per_page'];
		$uri = $this->uri->segment(3);

		//////////////////////////////////////////////////////
		/////////////////// end pagination /////x//////////////
		//////////////////////////////////////////////////////

		//////////////////////////////////////////////////////
		/////////////////////// data /////////////////////////
		//////////////////////////////////////////////////////

		$this->data['vdo'] = $this->favorite_model->get_favorite($per_page, $uri ,$this->data['member'][0]['mem_id']);		
		$config['total_rows'] = $this->favorite_model->count_get_favorite($this->data['member'][0]['mem_id']);
		$this->pagination->initialize($config); 

		$this->load->view('favorite' ,$this->data);
	}

	public function add_favorite()
	{
		$input = array(
			'mem_id' => $this->input->post('mem_id'),
			'vdo_id' => $this->input->post('vdo_id')
		);
		$data['check_favorite'] = $this->favorite_model->check_favorite($input['mem_id'] ,$input['vdo_id']);

		if($data['check_favorite'] == null)
		{
			$this->favorite_model->add_favorite($input);
		}		
	}

	public function delete_favorite()
	{
		$select_all = $this->input->post('select_all');

		for($i=0;$i<count($select_all);$i++)
		{
			if($select_all[$i] != "")
			{
				$id = $select_all[$i];
				$this->favorite_model->delete_favorite($id);
			}
		}
		redirect('favorite');
	}

	public function remove_favorite()
	{
		$input = array(
			'mem_id' => $this->input->post('mem_id'),
			'vdo_id' => $this->input->post('vdo_id')
		);
		$data['check_favorite'] = $this->favorite_model->check_favorite($input['mem_id'] ,$input['vdo_id']);

		$this->favorite_model->delete_favorite($data['check_favorite'][0]['fav_id']);
	}
}